// pages/recordDetail/recordDetail.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        current: "1",
        recordData1: [],
        recordData2: []
    },
    handleChange({detail: detail}) {
        var that = this;
        that.setData({
            current: detail.key
        });
        if (detail.key == "1") {
            that.getData(detail.key);
        } else if (detail.key == "2") {
            that.getData(detail.key);
        }
    },
    //获取记录
    getData(status) {
        var that = this;
        //recordData1、recordData2不为空数组，不请求接口，刷新下拉刷新
                if (status == "1" && that.data.recordData1.length == 0) {
            common.request({
                endpoint: "Integral/exchange/record_goods",
                method: "post",
                data: {
                    openid: wx.getStorageSync("user").openid
                },
                success(res) {
                    //console.log(res, "商品记录")
                    that.setData({
                        recordData1: res.extra
                    });
                }
            });
        } else if (status == "2" && that.data.recordData2.length == 0) {
            // 积分明细
            common.request({
                endpoint: "Integral/exchange/record",
                method: "post",
                data: {
                    openid: wx.getStorageSync("user").openid
                },
                success(res) {
                    //console.log(res, "积分记录")
                    var data = res.extra;
                    var totalIntegration;
                    data.forEach((item, index) => {
                        if (index == 0) {
                            totalIntegration = item.m_integ;
                        }
                        if (item.rec_integ >= 0) {
                            item.rec_integ = "+" + item.rec_integ;
                        }
                        item.extra_integral = Number(item.extra_integral);
                    });
                    that.setData({
                        recordData2: res.extra,
                        totalIntegration: totalIntegration
                    });
                }
            });
        }
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        var that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        var current = that.data.current;
        that.getData(1);
        that.getData(2);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {}
});